package com.lagou;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class IPersistenceTest {

    public static void main(String[] args) throws Exception{
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

        //1. 添加
        User user = new User();
        user.setUsername("linzhihong");
        user.setPassword("123");
        user.setBirthday("1997-06-01");

        int insert = userDao.insert(user);
        System.out.println("插入: " + insert);

        //2. 更新
        User newUser = new User();
        newUser.setId(4);
        newUser.setBirthday("1987-06-01");

        int update = userDao.update(newUser);
        System.out.println("更新: " + update);

        //3. 删除
        User disappearUser = new User();
        disappearUser.setId(3);

        int delete = userDao.delete(disappearUser);
        System.out.println("删除: " + delete);

        //4. 查询
        List<User> all = userDao.findAll();
        for (User user1 : all) {
            System.out.println(user1);
        }
    }
}
